﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Backend.JYD.Model.Rate;

namespace Backend.JYD.EFDAL.DAL
{
    public class RateModelDAL : IDAL.Rate.IRateModel
    {
        #region IRateModel 成员
        public long AddRateModel(RateModel model) {
            using (JydDataContext db = new JydDataContext())
            {
                //计算当前node的innercode值，先查询父节点的innercode
                model.innercode = getNodeInnerCode(model);
                model.top_innercode = model.innercode.Substring(0, 3);
                model.version = 1;
                db.RateModels.Add(model);
                db.SaveChanges();
                return model.id;
            }
        }

        public bool UpdateRateModel(RateModel model)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.id == model.id);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    //temp.parent_id = model.parent_id;
                    temp.code = model.code;
                    temp.name = model.name;
                    temp.dataclass_id = model.dataclass_id;
                    temp.weightable = model.weightable;
                    temp.showall = model.showall;
                    temp.necessity = model.necessity;
                    temp.isleaf = model.isleaf;
                    //temp.version = model.version + 1;
                    temp.input_type = model.input_type;
                    temp.rate_type = model.rate_type;
                    temp.caculate_class = model.caculate_class;
                    temp.dictionary_id = model.dictionary_id;
                    //....
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public bool CancelRateModel(Int64 modelId) {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.id == modelId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    if (temp.Status <= 1)
                    {
                        temp.Status = -1;
                        return db.SaveChanges() > 0;
                    }
                }
            }
            return false;
        }

        public RateModel GetRateModelByModelId(Int64 modelId) {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.id == modelId);
                var temp = query.FirstOrDefault();
                return temp;
            }
        }

        public IList<Model.Rate.RateModel> loadChildModels(Int64 modelId, Int64 version)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.parent_id == modelId && p.version == version);
                return query.ToList();
            }
        }

        public IList<RateModel> loadAllModels()
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.parent_id == null).OrderBy(p => p.name).OrderBy(p => p.version);
                return query.ToList();
            }
        }

        public bool SaveTree(RateModel model) {
            //获取根节点
            string strInnerCode = model.innercode;
            string upParentInnerCode = strInnerCode.Substring(0,3);
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.RateModels.Where(p => p.top_innercode == upParentInnerCode).OrderBy(p => p.code);
                IList<RateModel> listModel = query.ToList();
                for (int i = 0; i < listModel.Count;i++ )
                {
                    RateModel nowModel = listModel.ElementAt(i);
                    nowModel.id = -1;
                    nowModel.version = nowModel.version + 1;
                    nowModel.UpdateTime = DateTime.Now;
                    db.RateModels.Add(nowModel);
                    db.SaveChanges();
                }

            }
            return true;
        }

        private string getNodeInnerCode(RateModel model) {
            using (JydDataContext db = new JydDataContext())
            {
                string strInnerCode = "001";
                if (model.parent_id != null)
                {
                    var query = db.RateModels.Where(p => p.parent_id == model.parent_id).OrderBy(p => p.innercode);
                    var parentNode = query.LastOrDefault();
                    string parentInnerCode = parentNode.innercode;
                    int nowLayerInnerCode = int.Parse(parentInnerCode.Substring(parentInnerCode.Length - 3, 3));
                    string upLayerInnerCode = parentInnerCode.Substring(0, parentInnerCode.Length - 3);
                    if (nowLayerInnerCode < 9)
                    {
                        strInnerCode = upLayerInnerCode + "00" + (nowLayerInnerCode + 1);
                    }
                    else if (nowLayerInnerCode >= 9 && nowLayerInnerCode < 99)
                    {
                        strInnerCode = upLayerInnerCode + "0" + (nowLayerInnerCode + 1);
                    }
                    else
                    {
                        strInnerCode = upLayerInnerCode + (nowLayerInnerCode + 1);
                    }
                }
                else {
                    var query = db.RateModels.Where(p => p.parent_id == null).OrderBy(p => p.innercode);
                    var lastNode = query.LastOrDefault();
                    if (lastNode != null)
                    {
                        int maxInnerCode = int.Parse(lastNode.innercode);
                        if (maxInnerCode < 9)
                        {
                            strInnerCode = "00" + (maxInnerCode + 1);
                        }
                        else if (maxInnerCode >= 9 && maxInnerCode < 99)
                        {
                            strInnerCode = "0" + (maxInnerCode + 1);
                        }
                        else
                        {
                            strInnerCode = "" + (maxInnerCode + 1);
                        }
                    }
                }
                return strInnerCode;
            }
        }
        #endregion
    }
}